#include "../lvgl/lvgl.h"

static lv_obj_t* ID_ta = NULL;
static lv_obj_t* PWD_ta = NULL;
static lv_obj_t * kb = NULL;

void ShowKeyboard(lv_event_t *e)
{
    //获取当前的事件编码
    lv_event_code_t code = lv_event_get_code(e);

    //获得事件触发的元凶(textarea)事件发生在哪个区域
    lv_obj_t * ta = lv_event_get_target(e);

    //判断事件是否被选中(聚焦事件)
    if (code == LV_EVENT_FOCUSED)
    {
        //设置键盘的目标输入框
        lv_keyboard_set_textarea(kb,ta);
        //清除键盘的隐藏
        lv_obj_clear_flag(kb,LV_OBJ_FLAG_HIDDEN);

    }
    // 判断事件编码是否为失去聚焦（原本的选中被取消）事件
    if (code == LV_EVENT_DEFOCUSED)
    {
        // 让键盘的输入目标为空避免误触
        lv_keyboard_set_textarea(kb, NULL);

        // 隐藏键盘
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
    
  
}

void LogFun(lv_event_t * e)
{
    char msg[257]={0};
    snprintf(msg,sizeof(msg),"id:%s,passwd:%s",
    lv_textarea_get_text(ID_ta),
    lv_textarea_get_text(PWD_ta));

     printf("--%s--\n" , msg );

}

void LogIn(void)
{
    // 设置字体
    lv_obj_set_style_text_font( lv_scr_act()  , &lv_font_montserrat_32 , 0 );


    //先创建一个键盘,刚开始先隐藏键盘
    kb = lv_keyboard_create(lv_scr_act());
    lv_obj_set_align(kb,LV_ALIGN_BOTTOM_MID);
    lv_obj_add_flag(kb,LV_OBJ_FLAG_HIDDEN);

    //创建账号区域
    ID_ta = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ID_ta,400,80);
    lv_obj_align(ID_ta,LV_ALIGN_TOP_MID,0,20);
    lv_textarea_set_one_line(ID_ta , true );
    lv_textarea_set_placeholder_text(ID_ta, "USER ID");
    lv_obj_add_event_cb(ID_ta,ShowKeyboard,LV_EVENT_ALL,NULL);
    

    //创建密码区域
    PWD_ta = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(PWD_ta,400,80);
    lv_textarea_set_one_line(PWD_ta , true );
    lv_obj_align_to(PWD_ta,ID_ta,LV_ALIGN_OUT_BOTTOM_MID,0,20);
    lv_textarea_set_placeholder_text(PWD_ta, "USER PWD");
    lv_textarea_set_password_mode(PWD_ta,true);
    lv_textarea_set_password_bullet(PWD_ta, "*");
    lv_obj_add_event_cb(PWD_ta,ShowKeyboard,LV_EVENT_ALL,NULL);

    //创建登录按钮
    lv_obj_t * btn  = lv_btn_create(lv_scr_act());
    lv_obj_align_to(btn,PWD_ta,LV_ALIGN_OUT_BOTTOM_MID,0,20);
    lv_obj_set_size(btn,150,80);
    lv_obj_add_event_cb(btn,LogFun,LV_EVENT_CLICKED,NULL);

    //创建标签
    lv_obj_t * label = lv_label_create(btn);
    lv_obj_center(label);
    lv_label_set_text(label,"Log In");


}